package random.fox.p2p.presenter;

import random.fox.p2p.interactors.LoginInteractor;
import random.fox.p2p.interactors.OnLoginFinishedListener;
import random.fox.p2p.view.ILoginView;

/**
 * Created by delong on 2015/3/16.
 */
public class LoginPresenterImpl implements LoginPresenter, OnLoginFinishedListener {

    private ILoginView iLoginView;
    private LoginInteractor loginInteractor;


    public LoginPresenterImpl(ILoginView iLoginView, LoginInteractor loginInteractor) {
        this.iLoginView = iLoginView;
        this.loginInteractor = loginInteractor;
    }

    @Override
    public void init() {
        String[] arr = loginInteractor.init();
        if (arr != null) {
            iLoginView.fillData(arr[0], arr[1]);
        }
    }

    public void validate(final String name, final String pwd) {
        iLoginView.showProgress();
        loginInteractor.login(name, pwd, this);
    }

    @Override
    public void onUsernameError() {
        iLoginView.showMessage("用户名错误");
        iLoginView.hideProgress();
    }

    @Override
    public void onPasswordError() {
        iLoginView.hideProgress();
        iLoginView.showMessage("密码错误");
    }

    @Override
    public void onSuccess(String name, String password) {
        if (iLoginView.isSavePassword()) {
            loginInteractor.savePassword(name, password);
        } else {
            loginInteractor.clearPassword();
        }
        iLoginView.hideProgress();
        iLoginView.goToHome();
    }
}
